/********************************************/
// Creation History
// --------------------------------------------------------------
//	Company:			HTHK
//	NAME:				StaffProjectOptions.java
//    Title   :			Make the Staff_no coming into being a list box
//	FUNCTION:
//	BY:				Poly Li
//	DATE:				12/01/2004
// --------------------------------------------------------------
/*********************************************/

package tss.options;

import tss.common.Options2;

import java.util.ArrayList;
import java.sql.Connection;
import java.util.*;

import org.apache.log4j.Logger;

import com.ffpc.sql.*;

import tss.multiproject.*;

/**
 * A Class class.
 * <P>
 * 
 * @author HTHK
 */
public class MultiStaffOptions extends Options2 {
	private Logger logger = Logger.getLogger(MultiStaffOptions.class);

	/**
	 * Constructor
	 */
	public MultiStaffOptions() {
		this.load();
	}

	// protected ArrayList createOptions(Connection con)
	@Override
	public ArrayList createOptions(Connection con) throws Exception {
		ArrayList options = new ArrayList();
		EmployeeBean employee = EmployeeProcess.createBean();
		employee.setStaff_no("");
		EmployeeBean[] employees = EmployeeProcess.find(con, employee);

		Options2.Option option = null;
		for (int i = 0; i < employees.length; i++) {
			option = new Options2.Option(employees[i].getStaff_no(), employees[i].getAlias() + " " + employees[i].getSurname() + " ("
					+ employees[i].getStaff_no() + ")");

			options.add(i, option);
		}
		return options;
	}

	public String orderOptions() throws Exception {
		EmployeeBean employee = EmployeeProcess.createBean();
		employee.setStaff_no("");
		Connection con = null;
		Map taffName = null;
		ConnectionFactory cf = null;
		try {
			cf = new ConnectionFactory("fasp_tss");
			con = cf.getConnection();
			EmployeeBean[] employees = EmployeeProcess.find(con, employee);
			String option = null;
			taffName = new TreeMap();
			for (int i = 0; i < employees.length; i++) {
				String value = employees[i].getStaff_no();
				String description = employees[i].getAlias() + " " + employees[i].getSurname() + " (" + employees[i].getStaff_no() + ")";
				StringBuffer sb = new StringBuffer("   <option value=\"").append(value).append("\"").append(" >").append(description)
						.append("</option>\n");
				taffName.put(employees[i].getAlias() + " " + employees[i].getSurname(), sb.toString());
			}
		} catch (Exception e) {
			logger.error(e, e);
		} finally {
			cf.finalize();
		}

		return getOrderOptions(taffName);
	}

	public Vector valueOptions() {
		Vector valueOptions = new Vector();
		for (int i = 0; this.options != null && i < this.options.size(); i++) {
			Option option = (Option) options.get(i);
			String value_options[] = new String[1];
			value_options[0] = option.getValue();
			valueOptions.addElement(value_options);
		}
		return valueOptions;
	}

	public String getOrderOptions(Map map) {
		Collection entrySet = map.entrySet();
		Iterator it = entrySet.iterator();
		StringBuffer sb = new StringBuffer();
		while (it.hasNext()) {
			Map.Entry e = (Map.Entry) it.next();
			// logger.debug("Key = "+e.getKey()+", Value is : "+e.getValue());
			sb.append(e.getValue());
		}
		return sb.toString();
	}

}
